<template>
  <div class="user-list-container">
    <!-- 查询表单 -->
    <el-card shadow="never" class="search-card">
      <el-form :model="queryParams" ref="queryForm" label-width="80px">
        <el-row :gutter="20">
           <!-- <el-col :span="6">
            <el-form-item label="合作单位" prop="username">
              <companySelect />
            </el-form-item>
          </el-col>  -->
          <el-col :span="6">
            <el-form-item label="类型" prop="status">
              <el-select
                v-model="queryParams.is_type"
                placeholder="请选择类型"
                clearable
              >
                <el-option
                  v-for="item in statusOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6" style="text-align: right">
            <el-button type="primary" @click="handleQuery">查询</el-button>
            <el-button @click="handleReset">重置</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-card>

    <!-- 表格 -->
    <el-card shadow="never" class="table-card">
      <div style="display: flex; margin-bottom: 20px">
        <el-button type="primary" @click="openAddDialog">创建</el-button>
      </div>
      <el-table
        v-loading="loading"
        :data="tableData"
        border
        style="width: 100%"
      >
        <el-table-column prop="grade_id" label="ID" width="80" />
        <!-- <el-table-column prop="commercial_insurance_name" label="保险名称" /> -->
        <el-table-column prop="grade_name" label="标签名称" />
        <el-table-column prop="created_at" label="创建时间" />
        <el-table-column label="操作" width="180">
          <template #default="scope">
            <el-button link @click="handleTableOption('edit', scope.row)"
              >编辑</el-button
            >
            <el-button link type="danger" @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        class="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      />
    </el-card>
    <el-dialog
      v-model="dialogVisible"
      v-if="dialogVisible"
      title="积分商品"
      width="800"
      :before-close="handleCloseAddDialog"
    >
      <el-form
        :model="form"
        :rules="rules"
        ref="formRef"
        label-width="150px"
        class="demo-form"
      >
        <!-- 公司ID -->
        <el-form-item label="名称" prop="grade_name">
          <el-input v-model="form.grade_name" />
        </el-form-item>
       
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="handleCloseAddDialog">取消</el-button>
          <el-button type="primary" @click="submitForm">
            保存
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import tableMixin from "@/mixins/tableMixin";
import api from "@/request/api.js";
import upload from "@/components/upload.vue";

export default {
  name: "UserList",
  mixins: [tableMixin],
  components: { upload },
  data() {
    // 结束日期验证
    const validateEndDate = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请选择结束日期"));
      }
      if (
        this.form.start_at &&
        new Date(value) < new Date(this.form.start_at)
      ) {
        return callback(new Error("结束日期不能早于开始日期"));
      }
      callback();
    };

    return {
      companyChildList: {},
      //查询参数
      queryParams: { insuranceEis_typeffectiveStatus: 0 },
      apimodel: "classi",
      apival: "getList",
      // 状态选项
      statusOptions: [
        { value: 0, label: "全部" },
        { value: 1, label: "课程" },
        { value: 2, label: "老师" },
      ],   statusOptions1: [
        { value: 1, label: "课程" },
        { value: 2, label: "老师" },
      ],
      //创建修改弹窗
      dialogVisible: false,
      dialogType: "",
      form: {
        grade_name: "",
      },
      rules: {
        grade_name: [
          { required: true, message: "请输入名称", trigger: "change" },
        ],
      },
      companys: [],
    };
  },
  methods: {
    openAddDialog(option = "") {
      this.dialogVisible = true;
      this.dialogType = option;
    },

    // 加载公司数据

    handleCloseAddDialog() {
      this.dialogVisible = false;
      this.dialogType = "";
      this.form = {
        grade_name: "",
        is_type: "",
      };
      this.resetForm();
    },
    // 编辑
    handleTableOption(option, row) {
      if (option == "edit") {
        this.form = row;
        this.openAddDialog(option);
      }
    },

    // 删除
    handleDelete(row) {
      this.$confirm("确认删除吗?", "提示", {
        type: "warning",
      })
        .then(async () => {
          api.classi.del({
              grade_id: row.grade_id,
            })
            .then((res) => {
              if (res?.status == 200) {
                this.$message.success("删除成功");
                this.getTableData();
              }
            });
        })
        .catch(() => {});
    },
    submitForm() {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          // 表单验证通过，提交逻辑
          console.log("表单提交:", this.form);
          if (this.dialogType != "edit") {
            api.classi.create({ ...this.form }).then((res) => {
              if (res?.status == 200) {
                this.$message.success("创建成功");
                this.handleReset();
                this.handleCloseAddDialog();
              }
            });
          } else {
            api.classi.update({ ...this.form }).then((res) => {
              if (res?.status == 200) {
                this.$message.success("修改成功");
                this.handleReset();
                this.handleCloseAddDialog();
              }
            });
          }
          // 这里可以添加API调用等提交逻辑
        } else {
          console.log("表单验证失败");
          return false;
        }
      });
    },
    resetForm() {
      this.$refs.formRef.resetFields();
    },
    //选择地点
    handleRegionChange(val) {
      this.form.province_id = val.province;
      this.form.city_id = val.city;
      this.form.area_id = val.district;
    },
  },
};
</script>

<style scoped>
.user-list-container {
  padding: 20px;
}
.search-card {
  margin-bottom: 20px;
}
.table-card {
  margin-bottom: 20px;
}
.pagination {
  margin-top: 20px;
  text-align: right;
}
</style>
